﻿#include <stdio.h>
#include <string.h>
#include <stdint.h>


void* _memset(uint8_t* dst, uint8_t c, size_t size)
{
	uint8_t* bp = dst;
	uint8_t* ep = bp + size;

	if (size > 16)
	{
		uint8_t offset = (uintptr_t)dst % 8;
		uint64_t fourword = c;

		fourword |= fourword << 8;
		fourword |= fourword << 16;
		fourword |= fourword << 32;

		while (offset != 0)
		{
			*bp = c;
			++bp;
			--offset;
		}

		size_t blks = (ep - bp) / 8;

		switch (blks % 8)
		{
			do
			{
		default:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
		case 7:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
		case 6:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
		case 5:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
		case 4:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
		case 3:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
		case 2:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
		case 1:
			*(uint64_t*)bp = fourword;
			bp += 8;
			--blks;
			} while (blks != 0);
		}
	}
	while (bp != ep)
	{
		*bp = c;
		++bp;
	}
	return bp;
}

int main()
{
	uint8_t sr[123];
	memset(sr, 'a', 123);
	_memset(sr, 'c', 17);

	return 0;
}